<template>
  <div class="login-container">
    <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form" label-position="left">
      <div class="title-container">
        <h3 class="title">物联卡系统</h3>
      </div>

      <el-form-item prop="code" v-if="codeShow">
        <el-input prefix-icon="el-icon-office-building" v-model="loginForm.code" placeholder="请输入商户号"
          :validate-event="false" @keyup.enter.native="handleLogin"></el-input>
      </el-form-item>

      <el-form-item prop="account">
        <el-input prefix-icon="el-icon-s-custom" v-model="loginForm.account" placeholder="请输入账号" :validate-event="false"
          @keyup.enter.native="handleLogin"></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input prefix-icon="el-icon-lock" v-model="loginForm.password" placeholder="请输入密码" :validate-event="false"
          @keyup.enter.native="handleLogin"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" style="width:100%;margin-bottom:30px;" @click="handleLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginForm: {},
      loginRules: {
        code: [{ required: true, message: "请输入商户号" }],
        account: [{ required: true, message: "请输入账号" }],
        password: [
          { required: true, message: "请输入密码" },
          { min: 6, message: "密码至少6位数" }
        ],
      },
      codeShow: true
    };
  },
  created() { },
  mounted() {
    console.log("运行环境:", process.env.VUE_APP_RUN_ENV)
    this.changeCode()
  },
  methods: {
    handleLogin() {
      this.$refs.loginFormRef.validate(valid => {
        if (!valid) {
          return this.$message.error("请输入正确的验证信息");
        }
        if (this.loginCode) {
          this.loginForm.code = this.loginCode
        }
        this.$api.merchant.merchantLogin(this.loginForm, res => {
          if (!res.status) {
            return this.$message.error(res.message);
          }
          if (res.data.menus.length > 0) {
            return this.$message.error('登录失败,没有访问权限!')
          }
          window.localStorage.clear();
          localStorage.setItem("userData", JSON.stringify(res.data));
          let nowTime = (new Date).getTime()
          const token = {
            token: res.data.token,
            nowTime: nowTime,
          }
          localStorage.setItem("token", JSON.stringify(token));
          localStorage.setItem("code", this.loginForm.code);

          this.$router.setRouterPath()
          this.$router.push("/home");
        });
      });
    },
    changeCode() {
      let url = new URLSearchParams(window.location.search);
      let code = url.get('code')
      if (code == ''){
        code = process.env.DEFAULT_MERCHANT_CODE
      }
      if (!code || code == 'null') {
        return this.codeShow = true
      }
      this.loginForm.code = code
      this.codeShow = false
    }
  }
};
</script>
<style lang="less">
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #2d3a4b;
  overflow: hidden;
  box-sizing: border-box;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    box-sizing: inherit;

    .title-container {
      position: relative;

      .title {
        font-size: 26px;
        color: #eee;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
    }

    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;

      .el-button {
        padding: 14px 15px;
        font-size: 12px;
        border-radius: 5px;
        margin-bottom: 0 !important;
      }

      .el-form-item__content {
        line-height: 40px;
        position: relative;
        font-size: 14px;

        .el-input {
          display: inline-block;
          height: 47px;

          input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            padding-left: 35px;
            color: #fff;
            height: 47px;
            caret-color: #fff;
          }
        }
      }
    }
  }
}
</style>
